package com.os.osframe.scaffold.create;

import com.os.osframe.scaffold.domain.ScaffoldXmlTable;
import com.os.osframe.scaffold.util.PropertyConfigurerLoader;
import com.os.osframe.util.StringUtil;

import javax.annotation.Resource;
import java.io.File;
import java.util.List;

/**
 * Created by wangdc on 2015-4-27.
 */
public class CreateNavCode extends BaseCreate implements ICreateCode {
    public CreateNavCode(){

    }
    protected List<ScaffoldXmlTable> scaffoldXmlTableList;
    public CreateNavCode(List<ScaffoldXmlTable> scaffoldXmlTableList){
        this.scaffoldXmlTableList=scaffoldXmlTableList;
    }
    public void create(String packages)throws Exception{
        System.out.println("=====开始生成导航页面=====");
        //String path = replaceStr(new File("").getAbsolutePath(), "\\", "/");//获取项目的根路径，这里获取到的是softdp的路径
        String basePath = (String) getPropertyConfigurer().getProperty("osframe.file.path");//获取属性配置的服务器路径
        if (basePath.substring(basePath.length() - 1).equals("/")) {
            basePath = basePath.substring(basePath.length() - 1);
        }
        //System.out.println(path);//E:/java/intelijwork/softdp
        String targetFilePath=basePath;
        targetFilePath+="/auto/code/"+packages.replaceAll("\\.","/")+"/jsp";
        //System.out.println(targetFilePath);
        String tempPath = replaceStr(new File(this.getClass().getResource("").getPath()).getAbsolutePath(), "\\", "/");//获取当前类所在路径
        if(tempPath.indexOf("create")>-1){
            tempPath=tempPath.substring(0,tempPath.length()-7);
        }
        tempPath+="/nav";
        //System.out.println(tempPath);
        //xmlObjList= ReadXmlObj.getDataInfoByModule(module, packages);//获取列表对象
        createFile(tempPath,targetFilePath);
    }
    /**
     * 待替换变量
     * @param packages 模块包路径
     */
    @Override
    protected void stringDeal(String packages){
        replaceStrings[0]="${packages}";
        replaceStrings[1]="${packageSimple}";
        replaceStrings[2]="${packagePath}";


        //获取域模型信息

        //获取包简称
        String packagesSimple=null;
        String[] packagesSimpleArr=packages.split("\\.");

        for(int i=0;i<packagesSimpleArr.length;i++){
            if(StringUtil.isNull(packagesSimple)){
                packagesSimple=packagesSimpleArr[i];
            }else{
                packagesSimple+=packagesSimpleArr[i].substring(0, 1).toUpperCase()+packagesSimpleArr[i].substring(1);
            }
        }
        targetStrings[0]=packages;
        targetStrings[1]=packagesSimple;
        targetStrings[2]=packages.replaceAll("\\.","/");

    }

    /**
     *替换导航文本
     * @return
     * @throws Exception
     */

    protected String replaceCode(String srcText) throws Exception{
        if(scaffoldXmlTableList==null || scaffoldXmlTableList.isEmpty()){
            return "";
        }
//        <li ui-sref-active="active">
//        <a ui-sref="dashboardView">  值 直接表名+List
//        <i class="glyphicon glyphicon-calendar icon text-info-dker"></i>
//        <span class="font-bold" >
//
//        <fmt:message key="baseAngulrTemplate.view"/>
//        </span>
//        </a>
//        </li>

        StringBuffer sb=new StringBuffer();
        for(int i=0;i<scaffoldXmlTableList.size();i++){//针对多张表分别添加其导航
            ScaffoldXmlTable scaffoldXmlTable=scaffoldXmlTableList.get(i);
            //sb.append("\r\n").append("            ").append("<li><cite></cite><a href=\""+scaffoldXmlTable.getJavaName()+"/list.html\" target=\"rightFrame\">"+scaffoldXmlTable.getTableInfo()+"</a><i></i></li>");
            sb.append("\r\n").append("            ").append("<li ui-sref-active=\"active\">");
            sb.append("\r\n").append("                ").append("<a ui-sref=\""+scaffoldXmlTable.getJavaName()+"List\">");
            sb.append("\r\n").append("                    ").append("<i class=\"glyphicon glyphicon-chevron-right icon text-info-dker\"></i>");
            sb.append("\r\n").append("                    ").append("<span class=\"font-bold\" >");
            //sb.append("\r\n").append("                         ").append(scaffoldXmlTable.getTableInfo());
            sb.append("\r\n").append("                         ").append("<fmt:message key=\""+ scaffoldXmlTable.getJavaName()+".module\"/>");

            sb.append("\r\n").append("                    ").append("</span>");
            sb.append("\r\n").append("                 ").append("</a>");
            sb.append("\r\n").append("            ").append("</li>");
        }
        srcText=replaceStr(srcText,"${navJspCode}",sb.toString());
        return srcText;
    }

}
